<?php

namespace app\system\controller;

use dingtalk\Auth;
use dingtalk\util\crypto\DingtalkCrypt;
use dingtalk\util\Http;
use think\Controller;
use think\Request;

class AuthController extends Controller
{
    private $accessToken;
    private $dingtalkConfig;
    private $token;
    private $aesKey;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->accessToken = Auth::getAccessToken();
        $this->dingtalkConfig = Auth::getConfig();
        $this->token = '43HfszVy';
        $this->aesKey = 'TINyJOeI3BzLqsRy6cdjbzJBSVfpEzUB3dqeuulmu2Z';
    }
    public function index(){
        return $this->fetch();
    }
    /**
     * 注册地址
     */
    public function add()
    {
        $url = 'http://'.config('dingtalk.DOMAIN').'/system/auth/callback.html';
        $callBody = [
            "call_back_tag" => ["user_add_org", "user_modify_org", "user_leave_org", "org_admin_add", "org_admin_remove",
                "org_dept_create", "org_dept_modify", "org_dept_remove", "org_remove", "label_user_change",
                "label_conf_add", "label_conf_modify", "label_conf_del", "org_change", "chat_add_member",
                "chat_remove_member", "chat_quit", "chat_update_owner", "chat_update_title", "chat_disband",
                "chat_disband_microapp", "check_in", "bpms_task_change", "bpms_instance_change"],
            "token" => $this->token,
            "aes_key" => $this->aesKey,
            "url" => $url
        ];
        $response = Http::post('/call_back/register_call_back', ["access_token" => $this->accessToken], json_encode($callBody));
        dump($response);
    }

    /**
     * 查询检索
     */
    public function query(){
        $response = Http::get('/call_back/get_call_back', ["access_token" => $this->accessToken]);
        dump($response);
    }

    /**
     * 删除
     */
    public function delete(){
        $response = Http::get('/call_back/delete_call_back', ["access_token" => $this->accessToken]);
        dump($response);
    }
    /**
     * 回调地址
     */
    public function callback()
    {
        $data = $this->request->param();
        $dingtalkCrypt = new DingtalkCrypt($this->token,$this->aesKey,config('dingtalk.CORPID'));
        //DecryptMsg($signature, $timeStamp = null, $nonce, $encrypt, &$decryptMsg)
        $mydata='';
        $dingtalkCrypt->DecryptMsg($data['signature'],$data['timestamp'],$data['nonce'],$data['encrypt'],$mydata);
        trace('推送过来的数据','debug');
        trace($data,'debug');
        trace('解密后的数据','debug');
        trace($mydata,'debug');
        /**
         1、推送过来的数据
         [ debug ] array (
        'signature' => '42a9817872798dea8de0ce78adecac3937f5d213',
        'timestamp' => '1511955108712',
        'nonce' => '82afBDlk',
        'encrypt' => 'JHdkxevZmtLHOlfoUJwWz3WYeSOoDOxXJuTDULClhGg8ZkGEnKMe5vrCKp621kP4EjQ7PWvNvks6zupjWsrzKz1KWSfQLMKjJFFr9guEfRh4iOnh1PiPteON9UHDNetbPN5kcLbTygyeuSxV40NMArE8wXWZckWU9wgTAIRM2udtq2Pwcg44gq90K+wVPiXszJAKkvTt0cYD6vrpoRXSkw==',
        )
         2、success的加密数据
        [ debug ] {"msg_signature":"9bfa854f9032bde890f21f4627e004a66569ca63","encrypt":"BTk6FqTtcF2yOrdVPHM2F\/25S14JEcQ0dte5Zsl\/3VV5BBC7RKdK7DHB\/4YHnW3M93aQNMDhT9+EdOf\/SCGjgg==","timeStamp":"1511955108712","nonce":"82afBDlk"}
         */
        //推送success加密字符串
        $encryptMsg='';
        $dingtalkCrypt->EncryptMsg('success', $data['timestamp'], $data['nonce'], $encryptMsg);
        echo $encryptMsg;
    }

}
